<template>
    <div>
        测试webview
        <el-button id="postMessage" @click="postMessage">点击</el-button>
    </div>
</template>

<script>
export default {
    name: '',
    components: {
    },
    data() {
        return {}
    },
    created() {
    },
    mounted() {
        console.log('window.location.href', window.location.href)
        this.handle();
        //接收app传递来的数据
        window.requestData = function (msg) {
            console.log('html接收app的消息', msg)
            msg = JSON.parse(JSON.stringify(msg))
            console.log(msg.type);
            console.log(parmsObj.title);
            parmsObj.title = ' 再次测试uniapp'
        }
    },
    methods: {
        postMessage() {
            console.log('发送消息')
            uni.postMessage({
                data: {
                    action: 'postMessage2'
                }
            });
        },
        handle() {
            // 待触发 `UniAppJSBridgeReady` 事件后，即可调用 uni 的 API。
            document.addEventListener('UniAppJSBridgeReady', function () {
                // uni.postMessage({
                //     data: {
                //         action: 'message'
                //     }
                // });
                console.log('发送消息')
                uni.postMessage({
                    data: {
                        action: 'postMessage1'
                    }
                });

                uni.getEnv(function (res) {
                    console.log('当前环境：' + JSON.stringify(res));
                });

                uni.navigateTo({
                    url: '/pages/404/404'
                });

                // HTML 接受APP发送过来的消息 (H5端)
                console.log('添加监听')
                window.addEventListener('message', e => {
                    console.log(e, 'HTML接收APP发送过来的消息(H5端)');
                }, false)

                // document.querySelector('.btn-list').addEventListener('click', function (evt) {
                //     var target = evt.target;
                //     if (target.tagName === 'BUTTON') {
                //         var action = target.getAttribute('data-action');
                //         switch (action) {
                //             case 'switchTab':
                //                 uni.switchTab({
                //                     url: '/pages/tabBar/API/API'
                //                 });
                //                 break;
                //             case 'reLaunch':
                //                 uni.reLaunch({
                //                     url: '/pages/tabBar/component/component'
                //                 });
                //                 break;
                //             case 'navigateBack':
                //                 uni.navigateBack({
                //                     delta: 1
                //                 });
                //                 break;
                //             default:
                //                 uni[action]({
                //                     url: '/pages/component/button/button'
                //                 });
                //                 break;
                //         }
                //     }
                // });
                // document.getElementById('postMessage').addEventListener('click', function () {
                //     console.log('发送')
                //     uni.postMessage({
                //         data: {
                //             action: 'message'
                //         }
                //     });
                // });
            });
        }
    },
}
</script> 

<style scoped>

</style>
